package ipaintplugin.editors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

public class DrawingEditor extends EditorPart implements IDrawChangeListener {

	private Document doc;
	private boolean isPageModified = false;
	private Caretaker caretaker = new Caretaker();

	public DrawingEditor() {
	}

	public Document getDocument() {
		return doc;
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		isPageModified = false;

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		isPageModified = false;
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);

	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return isPageModified;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void createPartControl(Composite parent) {
		doc = new Document(parent, SWT.DOUBLE_BUFFERED);
		doc.setDrawChangeListener(this);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	public void redo() {
		if (caretaker.getFutureSize() > 0) {

			caretaker.pushPastMemento(new Memento(doc.getFigures()));
			doc.setFigures(caretaker.popFutureMemento().getSavedState());
			doc.redraw();
		}
	}

	public void undo() {
		if (caretaker.getPastSize() > 0) {
			caretaker.pushFutureMemento(new Memento(doc.getFigures()));
			doc.setFigures(caretaker.popPastMemento().getSavedState());
			doc.redraw();
		}
	}

	@Override
	public void change(Document doc) {
		if (doc == this.doc) {
			caretaker.pushPastMemento(new Memento(doc.getFiguresClone()));
		}
	}

}
